
#include "TestApp.h"

CTestApp::CTestApp()
{
	m_hInst = NULL;
	m_hWnd = NULL;
	m_pRenderSystem = NULL;
	m_pModel = NULL;
	m_pTexMgr = NULL;
}


CTestApp::~CTestApp()
{

}


void CTestApp::onEvent(MSG& msg)
{

}


bool CTestApp::updateFrame(int passedTime)
{
	if ( m_pTexMgr )
		m_pTexMgr->update( passedTime , passedTime );
	CSampleApp::updateFrame(passedTime);
	return true;
}
bool     CTestApp::initEnv()
{
#ifdef _WIN64
#ifdef _DEBUG
	GetLaborCore()->loadPlugin( L"LaborModelEx_Debug_x64.dll");
#else
	GetLaborCore()->loadPlugin( L"LaborModelEx_Release_x64.dll");
#endif
#else
#ifdef _DEBUG
	GetLaborCore()->loadPlugin( L"LaborModelEx_Debug_Win32.dll");
#else
	GetLaborCore()->loadPlugin( L"LaborModelEx_Release_Win32.dll");
#endif
#endif


	m_pTexMgr = xBaseTextureLoader::createInstance( m_pRenderSystem, L"BaseTexture" );
	//m_pModelMgr = new CLBaseModelMgr( L"BaseModel", 1);
	m_pModelMgr = CLModelLoader::createInstance(m_pTexMgr,  L"BaseModel" );
	m_pTexMgr->setThis(m_pTexMgr);
	//m_TextureMagrs.push_back(pMgr);
	m_pTexMgr->setMaxSize(256 * 1024 * 1024);

	std::wstring strTexPath = m_strCurrentPath;
	strTexPath += L"resource\\texture\\";
	std::wstring strModelPath = m_strCurrentPath;
	strModelPath += L"resource\\model\\";
	m_pTexMgr->addPath(  strTexPath.c_str() );
	m_pModelMgr->addPath(  strModelPath.c_str() );

	return true;
}

bool CTestApp::initApp( const char* appModuleFile )
{
	return CSampleApp::init( appModuleFile, L"Test03_AseModel", L"Test03 : AseModel", 800, 600 );
}

void CTestApp::initRenderData()
{
	ILGpuProgram *pGpuProgram = m_pRenderSystem->createGPUProgram();
	wstring strVS, strPS;
	strVS = m_strShaderPath;
	strPS = m_strShaderPath;

	strVS += L"StaticMesh.vertex.hlsl";
	strPS += L"StaticMesh.pixel.hlsl";

	pGpuProgram->load( strVS.c_str(), strPS.c_str(), NULL, 0);
	m_pRenderSystem->setGPUProgram( pGpuProgram );
	HRESULT hr = S_OK;

	// Initialize the world matrix
	//D3DXMatrixIdentity( &m_World );
	m_World.identity();
	// Initialize the view matrix
	m_Camera.setPosition( CLPoint3(0.0f, 0.0f, 6.0f) );
	m_Camera.lookAt( CLPoint3(0.0f, 0.0f, 0.0f) );

	m_hTexture = m_pTexMgr->add( L"seafloor.dds", 0 , true );
	m_hModel = m_pModelMgr->add(L"box.ase", 0, true);
	//// sample state 
	sLSamplerStateDesc _sampler_desc;
	ILSamplerState *pSamplerState = m_pRenderSystem->createSamplerState( _sampler_desc );
	m_pRenderSystem->setSamplerState( 0, pSamplerState );


	pGpuProgram->commit();
	m_pRenderSystem->setWorldMatrix( (float* )&m_World );
	CLMatrix4x4 viewMat = m_Camera.getViewMatrix();
	CLMatrix4x4 proMat  = m_Camera.getProjectionMatrix();

	m_pRenderSystem->setViewMatrix( (float* )&viewMat );
	m_pRenderSystem->setProMatrix( (float* )&proMat );

	m_pModel = m_hModel.getResource();
}


//--------------------------------------------------------------------------------------
// Render the frame
//--------------------------------------------------------------------------------------
void CTestApp::Render()
{
	if ( NULL == m_pRenderSystem )
		return;
	// Update our time
	static float t = 0.0f;

	static DWORD dwTimeStart = 0;
	DWORD dwTimeCur = GetTickCount();
	if( dwTimeStart == 0 )
		dwTimeStart = dwTimeCur;
	t = ( dwTimeCur - dwTimeStart ) / 1000.0f;

	CLTexture *pTexture = NULL;
	pTexture = (CLTexture *)m_hTexture.getResource();
	m_pRenderSystem->setTexture( pTexture );

	//Animate the cube

	m_World.rotY( t );
	m_pRenderSystem->clearBackBuffer(0.0f, 0.125f, 0.3f, 1.0f);
	m_pRenderSystem->setWorldMatrix( (float* )&m_World );


	m_pRenderSystem->begin();

	if ( m_pModel )
	{
		m_pModel->render();
	}
	m_pRenderSystem->present(0, 0);
	m_pRenderSystem->end();
}
